/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.rrym.common.utils;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 * 
 * @author Mark sunlightcs@gmail.com
 */
@Data
public class Result<T> extends HashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = 1L;

	private int code = 0;
	private String msg = "success";
	private T data;

	
	public Result() {
		put("code", 0);
	}

	public Result<T> ok(T data) {
		this.setData(data);
		return this;
	}

	public static Result ok(String msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}
	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}





	
	public static Result error(String msg) {
		return error(500, msg);
	}
	
	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}




	public boolean success(){
		return code == 0 ? true : false;
	}
	

	
	public static Result ok() {
		return new Result();
	}

	@Override
	public Result put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
